﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace OISTools
{
    public partial class XMLAnalyse : Form
    {
        public XMLAnalyse()
        {
            InitializeComponent();
        }

        string path=@"F:\工作相关\富高\AllnaturePgSQL\xmldata";
        DataAccess.SqlProvider db;
        private void btnRead_Click(object sender, EventArgs e)
        {
            setPath("apfield.xml");

            XmlDocument xmlDoc=new XmlDocument();
            xmlDoc.Load(path);


            List<apfield> apfieldList = new List<apfield>();

            foreach (XmlNode row in xmlDoc.ChildNodes[1].ChildNodes)
            {
                string ddl = "", join = "";
                if (row.SelectSingleNode("DDLConditions") != null)
                {
                    ddl = row.SelectSingleNode("DDLConditions").InnerText;
                }
                if (row.SelectSingleNode("JoinConditions") != null)
                {
                    join = row.SelectSingleNode("JoinConditions").InnerText;
                }
                if (ddl != "" || join != "")
                {
                    apfield obj = new apfield();
                    obj.DDLConditions = ddl;
                    obj.JoinConditions = join;
                    apfieldList.Add(obj);
                }

            }
            dataGridView1.DataSource = apfieldList;
        }




        private void setPath(string _path)
        {
            if (txtPath.Text != "")
                path = System.IO.Path.Combine(path, txtPath.Text);

            path = System.IO.Path.Combine(path, _path);
        }

        private void btnApmodelg_Click(object sender, EventArgs e)
        {
            setPath("apmodelg.xml");
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(path);
            List<apmodelg> list1=new List<apmodelg>();

            string sqlTemp = "insert into T_apmodelg(qryfilter,addcondition,editcondition,deletesql,maintitle,insertsql) values({0});";
            StringBuilder sqlTextColl = new StringBuilder();

            foreach (XmlNode row in xmlDoc.ChildNodes[1].ChildNodes)
            {
                apmodelg a = new apmodelg();
                a.qryfilter = getVallue(row.SelectSingleNode("qryfilter")).Replace("'", "''");
                a.addcondition = getVallue(row.SelectSingleNode("addcondition")).Replace("'", "''");
                a.editcondition = getVallue(row.SelectSingleNode("editcondition")).Replace("'", "''");
                a.deletesql = getVallue(row.SelectSingleNode("deletesql")).Replace("'", "''");
                a.maintitle = getVallue(row.SelectSingleNode("maintitle")).Replace("'", "''");
                a.insertsql = getVallue(row.SelectSingleNode("insertsql")).Replace("'", "''");
                list1.Add(a);
                string values = "'" + a.qryfilter + "','" + a.addcondition + "','" + a.editcondition + "','" + a.deletesql + "','" + a.maintitle + "','" + a.insertsql + "'";
                string sqlText = string.Format(sqlTemp, values);
                sqlTextColl.Append(sqlText);
            }

            dataGridView1.DataSource = list1;
            db.ExecuteSQL(sqlTextColl.ToString());
        }

        private string getVallue(XmlNode node)
        {
            if (node != null)
                return node.InnerText;
            return "";
        }




        public class apfield
        {
            public string DDLConditions { set; get; }
            public string JoinConditions { set; get; }
        }

        public class apmodelg
        {
            public string qryfilter     { set; get; }
            public string addcondition { set; get; }
            public string editcondition { set; get; }
            public string deletesql     { set; get; }
            public string maintitle     { set; get; }
            public string insertsql     { set; get; }
        }

        private void XMLAnalyse_Load(object sender, EventArgs e)
        {
            db = new DataAccess.SqlProvider(OisLibrary.DB.Connection);
        }
    }
}
